//
//  HNSH_HomeManager.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/25.
//

#define JXTableHeaderViewHeight 130
#define JXheightForHeaderInSection  46

#import "HNSH_HomeManager.h"
//view
#import "JXPagerView.h"
#import "JXCategoryTitleView.h"
#import "JXCategoryIndicatorLineView.h"
#import "HNSH_HomeNavView.h"

#import "HNSH_HomeManagerTopView.h"
#import "HNSH_TongZhengSuspensionView.h"

#import "HNSH_HomeManageModel.h"

//vc
#import "HNSH_HomeViewController.h"
#import "HNSH_HomeCommonViewController.h"
#import "HNSH_SearchGoodsViewController.h"

@interface HNSH_HomeManager ()<JXPagerViewDelegate, JXPagerMainTableViewGestureDelegate, JXCategoryViewDelegate>
/**  */
@property (nonatomic, strong) JXPagerView * pagerView;
/**  */
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
/**  */
@property (nonatomic, strong) HNSH_HomeManagerTopView * headerView;

/* manageModel */
@property (nonatomic, strong) HNSH_HomeManageModel *manageModel;

/* suspensionView */
@property (nonatomic, strong) HNSH_TongZhengSuspensionView *suspensionView;
/* showView */
@property (nonatomic, strong) HNSH_TongZhengSuspensionShowView *showView;

@end

@implementation HNSH_HomeManager

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self loadUserConsumeQuota];
}

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
        
    self.navView.backgroundColor = [UIColor clearColor];
        
    [self loadNewData];
    
    [self configTopView];
    
    [self configNav];
}

#pragma mark configUI
- (void)configTopView {
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+118)];
    imgV.image = [UIImage imageNamed:@"home_top_bg"];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];
}

- (void)configNav {
    HNSH_HomeNavView *nav = [[HNSH_HomeNavView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
    WeakSelf(weakSelf);
    nav.searchBlock = ^{
        HNSH_SearchGoodsViewController *vc = [[HNSH_SearchGoodsViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    [self.view addSubview:nav];
}

- (void)addAllViews{
    [self.view addSubview:self.pagerView];
    
    [self.view addSubview:self.showView];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
        [self.view addSubview:self.suspensionView];
    }
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

    self.pagerView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight);
}

//MARK: 获取所有代币
- (void)addCategoryViewWithArray:(NSArray *)ary {
    NSMutableArray *titles = [NSMutableArray arrayWithCapacity:ary.count];
    for (HNSH_HomeViewBannerItem *item in ary) {
        [titles addObject:item.name];
    }
    self.categoryView.titles = titles;
    [self.categoryView reloadDataWithoutListContainer];
    [self.pagerView reloadData];
    [self.view clipCornerWithView:self.categoryView cornerWidth:12 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
    
    [self addAllViews];
}

#pragma mark network
- (void)loadNewData {
    [HNSH_ShopNetServer requestGoodsHomeDataSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_HomeManageModel *model = [HNSH_HomeManageModel yy_modelWithDictionary:responseObject.resultset];
            self.manageModel = model;
            [self addCategoryViewWithArray:model.navigations];
            self.headerView.banners = model.banners;
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//消费额度信息
- (void)loadUserConsumeQuota {
    [HNSH_ShopNetServer requestUserConsumeQuotaSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSString *xfedString = [NSString stringWithFormat:@"消费额度：%.2f",[responseObject.resultset[@"consume_quota"] doubleValue]];;
            self.showView.titleLabel.text = xfedString;
            self.showView.xfedString = xfedString;
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (JXPagerView *)pagerView{
    if (!_pagerView) {
        _pagerView = [[JXPagerView alloc] initWithDelegate:self];
        _pagerView.mainTableView.gestureDelegate = self;
        _pagerView.mainTableView.backgroundColor = [UIColor clearColor];
        _pagerView.backgroundColor = [UIColor clearColor];
        _pagerView.mainTableView.bounces = NO;
    }
    return _pagerView;
}

- (HNSH_HomeManagerTopView *)headerView{
    if (!_headerView) {
        _headerView = [[HNSH_HomeManagerTopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, JXTableHeaderViewHeight)];
    }
    return _headerView;
}

- (HNSH_TongZhengSuspensionView *)suspensionView {
    if (!_suspensionView) {
        _suspensionView = [[HNSH_TongZhengSuspensionView alloc]initWithFrame:CGRectMake(kScreenWidth-80, kScreenHeight-kTabBarHeight-88-66, 80, 66)];
        WeakSelf(weakSelf);
        _suspensionView.showBlock = ^(BOOL showView) {
            if (showView) {
                weakSelf.showView.hidden = NO;
                [UIView animateWithDuration:0.5 animations:^{
                    weakSelf.showView.frame = CGRectMake(kScreenWidth-weakSelf.showView.width-28, weakSelf.showView.origin.y, weakSelf.showView.width, weakSelf.showView.height);
                }];
            } else {
                [UIView animateWithDuration:0.5 animations:^{
                    weakSelf.showView.frame = CGRectMake(kScreenWidth, weakSelf.showView.origin.y, weakSelf.showView.width, weakSelf.showView.height);
                } completion:^(BOOL finished) {
                    weakSelf.showView.hidden = YES;
                }];
            }
        };
        _suspensionView.closeBlock = ^{
            [weakSelf.showView removeFromSuperview];
        };
    }
    return _suspensionView;
}

- (HNSH_TongZhengSuspensionShowView *)showView {
    if (!_showView) {
        _showView = [[HNSH_TongZhengSuspensionShowView alloc]initWithFrame:CGRectMake(kScreenWidth, kScreenHeight-kTabBarHeight-88-32, 210, 32)];
//        _showView.titleLabel.text = [NSString stringWithFormat:@"消费额度：%.2f",[self.xfedString doubleValue]];
        _showView.hidden = YES;
    }
    return _showView;
}

- (JXCategoryTitleView *)categoryView{
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, JXheightForHeaderInSection)];
        _categoryView.backgroundColor = [UIColor clearColor];
        _categoryView.delegate = self;
        _categoryView.titleSelectedColor = [UIColor appBlackLabelColor];
        _categoryView.titleColor = [UIColor appBlackLabelColor];
        _categoryView.titleFont = [UIFont systemFontOfSize:14];
        _categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
        _categoryView.titleColorGradientEnabled = YES;
        _categoryView.titleLabelZoomEnabled = YES;
        _categoryView.contentScrollViewClickTransitionAnimationEnabled = NO;
        _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;

        JXCategoryIndicatorLineView * lineView = [JXCategoryIndicatorLineView new];
        lineView.indicatorColor = [UIColor appGreenColor];
        lineView.indicatorWidth = 20;
        _categoryView.indicators = @[lineView];
    }
    return _categoryView;
}

//MARK: JXPagerViewDelegate Methods
- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView{
    return self.headerView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView{
    return JXTableHeaderViewHeight;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView{
    return JXheightForHeaderInSection;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView{
    return self.categoryView;
}

- (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView{
    //和categoryView的item数量一致
    return self.categoryView.titles.count;
}

- (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index{
    if (index == 0) {
        HNSH_HomeViewController * list = [HNSH_HomeViewController new];
        list.type = index;
        return list;
    } else {
        HNSH_HomeCommonViewController * list = [HNSH_HomeCommonViewController new];
        HNSH_HomeViewBannerItem *item = self.manageModel.navigations[index];
        list.type = [item.jump_params integerValue];
        return list;
    }
    
}

//MARK: JXCategoryViewDelegate Methods
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index{
    
}

//MARK: JXPagerMainTableViewGestureDelegate Methods
- (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //禁止categoryView左右滑动的时候，上下和左右都可以滚动
    if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
        return NO;
    }
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}



@end
